package algorithm.test;

public class JosephRing {
	public static void main(String[] args) {
		JosephRing jr = new JosephRing();
		Ring r = jr.new Ring();
		// suppose there is some kids
		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
		r.killAll(arr, 3);// every counting to 3 to kill one kid
	}

	class Ring {
		public void killAll(int[] arr, int count) {
			int c, kc, i;
			for (c = kc = i = 0; /* i < arr.length */; i = ++i % arr.length) {
				if (kc >= arr.length)
					break;
				if (arr[i] == -1)
					continue;
				if (++c >= count) {
					System.out.println(arr[i] + " down ");
					arr[i] = -1;// -1 indicating one killed
					c = 0;// every counting to "count" set c = 0
					kc++;// very killing one, kc increasing
				}
			}
		}
	}
}
